namespace IconPatcher
{
    using System;

    using IconPatcher.Annotations;

    using ManyConsole;

    public abstract class BetterConsoleCommand : ConsoleCommand
    {
        private bool Logging;

        protected BetterConsoleCommand()
        {
            SkipsCommandSummaryBeforeRunning();

            HasOption("l|log", "Turn on the display of messages when a DLL wasn't found.", a => Logging = true);
        }

        [StringFormatMethod("message")]
        public static void ErrorMessage(string message, params object[] args)
        {
            var before = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message, args);

            Console.ForegroundColor = before;
        }

        [StringFormatMethod("message")]
        public static void SuccessMessage(string message, params object[] args)
        {
            var before = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(message, args);

            Console.ForegroundColor = before;
        }

        [StringFormatMethod("message")]
        protected void LogMessage(string message, params object[] args)
        {
            if (!Logging)
            {
                return;
            }

            Console.WriteLine(message, args);
        }

        protected string[] GetLanguagess(VisualStudioVersion version)
        {
            switch (version)
            {
                case VisualStudioVersion.VS2010:
                    return Program.Languages2010;

                case VisualStudioVersion.VS2012:
                    return Program.Languages2012;

                default:
                    throw new ArgumentOutOfRangeException("version");
            }
        }

        protected bool ForceVisualStudioToBeClosed(string cancelMessage)
        {
            while (Helpers.IsVisualStudioRunning())
            {
                ErrorMessage("You must close all instances of Visual Studio. Press the any key to continue or escape to cancel.");
                Console.WriteLine();

                var key = Console.ReadKey(true);
                if (key.KeyChar == 27)
                {
                    ErrorMessage(cancelMessage);
                    return false;
                }
            }

            return true;
        }
    }
}